package com.jbookshelf.beans;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.context.RequestContext;
import org.primefaces.model.DualListModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.jbookshelf.dal.entities.Permissions;
import com.jbookshelf.dal.entities.Users;
import com.jbookshelf.dal.providers.UsersDataProvider;

@Component
@Scope("request")
public class UsersBean {

	private Users newUser;
	private List<Users> users;
	private Users selectedUser;
	@Autowired
	UsersDataProvider provider;

	@Autowired
	private PermissionsBean permissionsBean;

	public UsersBean() {
		selectedUser = new Users();
		newUser = new Users();
	}

	public Users getNewUser() {
		return newUser;
	}

	public void setFirstname(String str) {
		newUser.setFirstName(str);
	}

	public void setLastname(String str) {
		newUser.setLastName(str);
	}

	public void setLogin(String str) {
		newUser.setLogin(str);
	}

	public void setEmail(String str) {
		newUser.setEmail(str);
	}

	public void setPassword(String str) {
		newUser.setPassword(str);
	}

	public String getFirstname() {
		return newUser.getFirstName();
	}

	public String getLastname() {
		return newUser.getLastName();
	}

	public String getLogin() {
		return newUser.getLogin();
	}

	public String getEmail() {
		return newUser.getEmail();
	}

	public void setIsBlocked(boolean blocked) {
		this.newUser.setIsBlocked(blocked);
	}

	public boolean getIsBlocked() {
		return newUser.isIsBlocked();
	}

	public String getPassword() {
		return newUser.getPassword();
	}

	public Users getSelectedUser() {
		return selectedUser;
	}

	public String getID() {
		return "" + newUser.getId();
	}

	public void setID(String id) {
		this.newUser.setId(Integer.parseInt(id));
	}

	public void setSelectedUser(Users selectedUser) {
		this.selectedUser = selectedUser;
	}

	public List<Users> getUsers() {
		if (users == null) {
			users = provider.getUsers();
		}
		return users;
	}

	public void setUsers(List<Users> users) {
		this.users = users;
	}

	public void saveNewUser() {
		RequestContext context = RequestContext.getCurrentInstance();
		provider.saveUser(newUser);
		FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
				"User added successfully", newUser.getLogin());
		context.update("users_list_form");
		FacesContext.getCurrentInstance().addMessage(null, msg);
		context.addCallbackParam("saveUser", true);
	}

	public void showCreateDialog() {
		final RequestContext context = RequestContext.getCurrentInstance();
		context.execute("createdlg.show()");
	}

	public void showEditDialog() {
		if (users.contains(selectedUser)) {
			newUser = selectedUser;
			final RequestContext context = RequestContext.getCurrentInstance();
			context.execute("editdlg.show()");
		} else {
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage("Failed!",
					"No user selected!"));
		}
	}

	public void saveEditUser(ActionEvent event) {
		RequestContext context = RequestContext.getCurrentInstance();
		Users user = provider.getUserById(selectedUser.getId());
		if (!user.getLogin().equals(getLogin())){
			if (provider.getUserByLogin(getLogin())!=null)
			{
				FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,
						"Login already exist", null);
				FacesContext.getCurrentInstance().addMessage(null,msg);
				return;
			}
		}
		
		if (!user.getEmail().equals(getEmail())){
			Users u = provider.getUserByEmail(getEmail());
			if (u!=null)
			{
				FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,
						"Email already exist", null);
				FacesContext.getCurrentInstance().addMessage(null,msg);
				return;
			}
		}	
		newUser.setId(user.getId());
		newUser.setPassword(user.getPassword());
		provider.updateUser(newUser);
		FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
				"User edited successfully", newUser.getLogin());

		FacesContext.getCurrentInstance().addMessage(null, msg);
		context.addCallbackParam("editUser", true);
	}

	public void showViewDialog() {
		if (users.contains(selectedUser)) {
			final RequestContext context = RequestContext.getCurrentInstance();
			context.execute("viewdlg.show()");
		} else {
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage("Failed!",
					"No user selected!"));
		}
	}

	public void showBlockDialog() {
		if (users.contains(selectedUser)) {
			final RequestContext context = RequestContext.getCurrentInstance();
			context.execute("blockdlg.show()");
		} else {
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage("Failed!",
					"No user selected!"));
		}
	}

	public void blockUser() {
		Users user = provider.getUserById(selectedUser.getId());
		user.setIsBlocked(true);
		provider.updateUser(user);
		closeBlockDialog();
	}

	public void closeBlockDialog() {
		final RequestContext context = RequestContext.getCurrentInstance();
		context.execute("blockdlg.hide()");
	}

	public void showPermissionsDialog() {
		if (users.contains(selectedUser)) {
			permissionsBean.editPermissions(selectedUser.getId());
		} else {
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage("Failed!",
					"No user selected!"));
		}

	}

	public void savePermissions() {
		DualListModel<Permissions> model = permissionsBean.getPermissions();
		permissionsBean.saveUserPermissions(model.getTarget(),
				selectedUser.getId());
	}
}
